from flask import Blueprint,render_template, request,session,redirect, url_for
from config import conn,cur,mutex ,r as redis

Login = Blueprint('Login',__name__)

@Login.route('/',methods=['GET','POST'])
def index():
    return render_template('/login/login.html')

@Login.route('/loginPatient', methods=['GET', 'POST'])
def loginPatient():
    # zwd:下面的内容只是个大概，具体细节后续添加
    id = request.form.get('u')
    password = request.form.get('p')

    #insert into personal_info (name,password) values ('zwd','123');
    sqlstr='select id,name from personal_info  where id= \''+ id +'\' and password=  \''+ password +'\''
    print(sqlstr)
    cur.execute(sqlstr)
    row = cur.fetchone()
    print(row)

    # zwd:一定要写！用session来记录登录信息
    if row is not None:
        session['id'] = row[0]
        session['name'] = row[1]
        session['role'] = 'patient'
        #result = session.get(‘key’) 
    # 路由重定向
    if row is not None:
        return {'message':"success!"}
        #return redirect('/VitalSigns/',code=301)
    else:
        return {'message':"failed!"}
        #return redirect('/Login/',code = 301)

@Login.route('/loginDoctor', methods=['GET', 'POST'])
def loginDoctor():
    # zwd:下面的内容只是个大概，具体细节后续添加
    id = request.form.get('u')
    password = request.form.get('p')

    #insert into personal_info (name,password) values ('zwd','123');
    sqlstr='select id,name from doctor_info  where id= \''+ id +'\' and password=  \''+ password +'\''
    print(sqlstr)
    cur.execute(sqlstr)
    row = cur.fetchone()
    print(row)

    # zwd:一定要写！用session来记录登录信息
    if row is not None:
        session['id'] = row[0]
        session['name'] = row[1]
        session['role'] = 'doctor'
        #result = session.get(‘key’) 
    # 路由重定向
    if row is not None:
        return {'message':"success!"}
        #return redirect('/VitalSigns/',code=301)
    else:
        return {'message':"failed!"}
        #return redirect('/Login/',code = 301)

@Login.before_request
def before():
    if session.get('id') is not None and session.get('role') == 'patient':
        return redirect('/VitalSigns/',code=301)
    elif session.get('id') is not None and session.get('role') == 'doctor':
        return redirect('/Doctor/', code=301)
    else:
        pass